package top.ajunnihao.core.util;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * TODO 网站临时文件获取工具类
 *
 * @author AJun
 * @version 1.0.0
 * @date 2020/6/1
 */
public class TmpFile {

    // 临时文件目录 : webapp\tmp\
    public static File getDir(HttpServletRequest request) {
        return new File(request.getServletContext().getRealPath("/tmp"));
    }

    // 获取临时文件
    public static File getFile(HttpServletRequest request, String tmpName) {
        File dir = getDir(request);
        dir.mkdirs();
        return new File(dir, tmpName);
    }

    // 获取临时文件的访问URL
    public static String getUrl(HttpServletRequest request, String tmpName) {
        return request.getContextPath() + "/tmp/" + tmpName;
    }

}
